
const mssql =  require('../lib/mssql')
const sqlite = require('../lib/sqlite')
const config =  require('../config/sqlite')

const tables = require('../config/tables')
//require the module
var sql = require('sql');

let value = 
{
  "channel": 0.82,
  "scale": 385,
  "area": "1_2",
  "cabinets": {
    "height": 2.43,
    "deep": 2.69,
    "offset": {
      "x": 0.2775,
      "y": 1.5365
    },
    "fix": 0,
    "item": [
      {
        "wight": 0.55,
        "row": 6,
        "slip": 3
      },
      {
        "wight": 0.55,
        "row": 6,
        "slip": 3
      },
      {
        "wight": 0.55,
        "row": 6,
        "slip": 3
      },
      {
        "wight": 0.55,
        "row": 6,
        "slip": 3
      }
    ]
  }
};




class SetupController {

  static async renew(ctx){
  
  }


  static async update(room, area,data){
  
    console.log(room);
    let query = tables.setup.update({data: JSON.stringify(data)}).where(tables.setup.area.equals(area), tables.setup.room.equals(room)).toQuery();
  
        console.log(query.text);
    return await sqlite(query.text, query.values);
    /*
    
      
  return;
    */
  }
  static async init(room, area,data){
  
    console.log(room);
    let query = tables.setup.insert(tables.setup.area.value(area), tables.setup.room.value(room), tables.setup.data.value(JSON.stringify(data))).toQuery();
    //ctx.body = query.values;
    await sqlite(query.text, query.values);
    query = tables.setup.select(tables.setup.data).where(tables.setup.area.equals(area), tables.setup.room.equals(room)).toQuery();
    return await sqlite(query.text, query.values);
    /*
    
      
  return;
    */
  }
  static async get(ctx){
    if(ctx.params.area != null){
        sql.setDialect('sqlite');
        var query = tables.setup.select(tables.setup.data).where(tables.setup.area.equals(ctx.params.area), tables.setup.room.equals(ctx.params.room)).toQuery();
        
        var data = await sqlite(query.text, query.values);
        ctx.body = data.length;
      
       
        if(data.length == 1){
            ctx.body = data[0].data;
        }else{
           ctx.body = false;
        }
    }
  }
  
    
  
  static async areas(ctx){
    const set = new Set();
    if(ctx.params.area != null){
        sql.setDialect('sqlite');
        var query = tables.setup.select(tables.setup.data).where(tables.setup.area.equals(ctx.params.area), tables.setup.room.equals(ctx.params.room)).toQuery();
        
        var data = await sqlite(query.text, query.values);
        return JSON.parse(data[0].data).cabinets.areas;
      
    }
    return [];
  }
  static async put(ctx){

    if(ctx.params.area != null){
        let query = tables.setup.select(tables.setup.data).where(tables.setup.area.equals(ctx.params.area), tables.setup.room.equals(ctx.params.room)).toQuery();
        let data = await sqlite(query.text, query.values);

     
        
        if(data.length == 0){
            data = await SetupController.init(ctx.params.room, ctx.params.area, ctx.request.body);
        }else{
            data = await SetupController.update(ctx.params.room, ctx.params.area, ctx.request.body);
        }
        
        console.log(ctx.request.body);
     
   
       
        ctx.body = data;//language.text; 
        
    }
     
   
  }
 
}

module.exports = SetupController